package com.lap.auth.domain.role.service;

import com.lap.auth.domain.role.Role;
import com.lap.auth.domain.role.repository.RoleRepository;
import com.lap.auth.domain.shared.errors.UserError;
import com.lap.framework.exception.BizException;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class RoleValidator {

  private final RoleRepository roleRepository;

  public Role assertRoleExists(Integer id) {
    return roleRepository
        .findById(id)
        .orElseThrow(() -> BizException.newBiz(UserError.ROLE_NOT_FOUND));
  }

  public void validateCodeUnique(String code) {
    boolean exists = roleRepository.existsByCode(code);
    if (exists) BizException.interrupt(UserError.ROLE_EXISTS);
  }
}
